﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class product_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            int _user_id = DTRequest.GetFormInt("txtUserID", 0);

            BLL.product bll = new BLL.product();
            Model.product model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"user_id\":" + model.user_id);
            strTxt.Append(",\"title\":\"" + model.title + "\"");
            strTxt.Append(",\"category_name\":\"" + new BLL.product_category().GetTitle(model.category_id) + "\"");
            if (model.price > 0)
            {
                strTxt.Append(",\"market_price\":\"" + model.price.ToString() + "元/" + model.unit.ToString() + "\"");
            }
            else
            {
                strTxt.Append(",\"market_price\":\"面议\"");
            }
            strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\"");
            strTxt.Append(",\"remark\":\"" + model.content.ToString() + "\"");

            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                    strTxt.Append(",\"lianxiren\":\"" + userModel.user_name + "\"");
                    if (userModel.avatar.Contains("shicaiquanzi"))
                    {
                        if (userModel.avatar.Contains("http://")) { strTxt.Append(",\"avatar\":\"" + userModel.avatar + "\""); }
                        else { strTxt.Append(",\"avatar\":\"http://" + userModel.avatar + "\""); }

                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + userModel.avatar + "\"");
                    }
                    //strTxt.Append(",\"avatar\":\"http://www.shicaiquanzi.com" + userModel.avatar + "\"");
                    strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                }
                //string _username = new BLL.users().GetUserName(model.user_id);
                //string _useravatar=new
            }
            else
            {
                strTxt.Append(",\"lianxiren\":\"\"");
                strTxt.Append(",\"avatar\":\"\"");
                strTxt.Append(",\"mobile\":\"\"");
            }

            string imglist = string.Empty;
            if (model.albums != null)
            {
                foreach (Model.common_albums modelt in model.albums)
                {
                    if (modelt.original_path.Contains("shicaiquanzi"))
                    {
                        if (modelt.original_path.Contains("http://")) {
                            imglist += "<div class=\"pic\"><img src=\"" + modelt.original_path + "\"/></div>";
                          }
                        else {  imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>"; }

                    }
                    else
                    {
                        imglist += "<div class=\"pic\"><img src=\"http://www.shicaiquanzi.com" + modelt.original_path + "\"/></div>";
                    }


                    //if (modelt.original_path.Contains("shicaiquanzi"))
                    //{
                    //    imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>";
                    //}
                    //else
                    //{
                    //    imglist += "<div class=\"pic\"><img src=\"http://www.shicaiquanzi.com" + modelt.original_path + "\"/></div>";
                    //}
                }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append(",\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            strTxt.Append(",\"hits\":" + model.hits);

            //判断是否有传过来用户名、密码
            if (new BLL.collection().Exists(3, id, _user_id))
            {
                strTxt.Append(",\"is_collect\":1");
            }
            else
            {
                strTxt.Append(",\"is_collect\":0");
            }
            strTxt.Append(",\"_user_id\":" + _user_id);
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}